package ipebg.mzk.common.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Properties;

 

public class PropertyPhase {
	/**
	 * 指定property文件
	 */
	private static final String PROPERTY_FILE = "config.properties";
	private static HashMap<String, String> propertys=new HashMap<String, String>();
	/**
	 * 根据Key 读取Value
	 * 
	 * @param key
	 * @return
	 */
	public static String readData(String key) {
		if(propertys.containsKey(key)){
			return propertys.get(key);
		}else{
			Properties props = new Properties();
			try {			
				InputStream in =PropertyPhase.class.getClassLoader().getResourceAsStream(PROPERTY_FILE);			
				BufferedReader bf= new BufferedReader(new InputStreamReader(in));
				props.load(bf);
				//props.load(in);
				in.close();
				String value = props.getProperty(key,"UTF-8");
				propertys.put(key, value);
				return value;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}		
	}

//	/**
//	 * 修改或添加键值对 如果key存在，修改 反之，添加。
//	 * 
//	 * @param key
//	 * @param value
//	 */
//	public static void writeData(String key, String value) {
//		Properties prop = new Properties();
//		try {
//			File file = new File(PROPERTY_FILE);
//			if (!file.exists())
//				file.createNewFile();
//			InputStream fis = new FileInputStream(file);
//			prop.load(fis);
//			fis.close();// 一定要在修改值之前关闭fis
//			OutputStream fos = new FileOutputStream(PROPERTY_FILE);
//			prop.setProperty(key, value);
//			prop.store(fos, "Update '" + key + "' value");
//			fos.close();
//		} catch (IOException e) {
//			System.err.println("Visit " + PROPERTY_FILE + " for updating " + value + " value error");
//		}
//	}
}
